﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using Speakup;
using System.Windows;
using System.Windows.Controls.Primitives;
using TableTypes;

namespace SpeakupUI
{
    class FilterPanel: ContentControl
    {
        static List<ClassType> SupportedTypes;
        static FilterPanel()
        {
            SupportedTypes = new List<ClassType>
            {
                ClassType.Club, 
                ClassType.Group, 
                ClassType.Grammar, 
                ClassType.Reading, 
                ClassType.Writing, 
                ClassType.Conversation                
            };
        }

        private ClassPanelList ClassPanelList;       

        private StackPanel ManualFilterPanel;
        private CheckBox ManualFilterCheckBox;

        private Dictionary<ClassType, CheckBox> ManualFilters = new Dictionary<ClassType, CheckBox>();

        public bool m_useManualFilter = true;
        public bool UseManualFilter
        {
            get
            {
                return m_useManualFilter;
            }
            set
            {
                m_useManualFilter = value;
                ManualFilterPanel.SetValue(UIElement.IsEnabledProperty, false);
            }
        }

        public FilterPanel(ClassPanelList classList)
        {
            ClassPanelList = classList;

            ManualFilterPanel = new StackPanel();
            ManualFilterPanel.HorizontalAlignment = HorizontalAlignment.Center;

            ManualFilterCheckBox = CreateCheckBox("Enable Manual Filter");
            ManualFilterCheckBox.Checked += EnableManualFilter;
            ManualFilterCheckBox.Unchecked += EnableManualFilter;
            ManualFilterCheckBox.IsChecked = !ClassPanelList.UseAutoFilter;

            foreach(ClassType type in SupportedTypes)
            {
                CheckBox chkBox = CreateCheckBox(ClassPanel.ClassTypeTitles[type]);
                chkBox.IsChecked = true;                                
                chkBox.Checked += FilterChanged;
                chkBox.Unchecked += FilterChanged;

                ManualFilters[type] = chkBox;
                ManualFilterPanel.Children.Add(chkBox);
            }

            GroupBox box = new GroupBox();
            box.Header = ManualFilterCheckBox;                  
            box.Content = ManualFilterPanel;
            Content = box;
        }
             

        public CheckBox CreateCheckBox(String title)
        {
            Label label = new Label();
            label.Content = title;
            CheckBox box = new CheckBox();
            box.IsChecked = true;
            box.Content = label;
            box.VerticalAlignment = VerticalAlignment.Center;            

            return box;
        }

        void EnableManualFilter(object sender, RoutedEventArgs e)
        {
            if (ManualFilterCheckBox.IsChecked == true)
            {
                ManualFilterPanel.Visibility = Visibility.Visible;
                ClassPanelList.UseAutoFilter = false;
            }
            else
            {
                ManualFilterPanel.Visibility = Visibility.Collapsed;
                ClassPanelList.UseAutoFilter = true;
            }
        }

        void FilterChanged(object sender, RoutedEventArgs e)
        {
            foreach (var pair in ManualFilters)
            {
                bool filter = pair.Value.IsChecked ?? true;
                ClassPanelList.SetFilter(pair.Key, filter);
            }
        }
    }
}
